package com.cytech.cloudclinic.dialog;

import android.app.AlertDialog;
import android.app.Dialog;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.DialogFragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.cytech.cloudclinic.R;
import com.cytech.cloudclinic.adapter.BillAdapter;
import com.cytech.cloudclinic.bean.CustomGroupDrug;
import com.cytech.cloudclinic.bean.Drug;
import com.cytech.cloudclinic.bean.Patient;
import com.cytech.cloudclinic.bean.req.AddPrescription;
import com.cytech.cloudclinic.utils.TimeUtils;
import com.yechaoa.yutils.YUtils;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 创建处方收费单
 */
public class PrescriptionListDialog extends DialogFragment {

    @BindView(R.id.rv_create_bill)
    RecyclerView rvPrescription;

    @BindView(R.id.tv_prescription_list_name)
    TextView tvPatientName;
    @BindView(R.id.tv_prescription_list_sex)
    TextView tvPatientSex;
    @BindView(R.id.tv_prescription_list_age)
    TextView tvPatientAge;

    @BindView(R.id.tv_prescription_list_now)
    TextView tvAction;
    @BindView(R.id.tv_prescription_list_history)
    TextView tvHistory;
    @BindView(R.id.tv_prescription_list_allergy)
    TextView tvAllergy;
    @BindView(R.id.tv_prescription_list_dosage)
    TextView tvDosage;

    private AddPrescription addPrescription;
    private Patient patient;
    private String prescriptionId;

    private List<Drug> mDrugSelectList;
    private BillAdapter billAdapter;

    public PrescriptionListDialog(AddPrescription addPrescription, Patient patient,String prescriptionId) {
        Bundle bundle = new Bundle();
        bundle.putSerializable("prescription",addPrescription);
        bundle.putSerializable("patient",patient);
        bundle.putString("prescriptionId",prescriptionId);
        setArguments(bundle);
    }

    @Override
    public void onStart() {
        super.onStart();
        WindowManager.LayoutParams lp = getDialog().getWindow().getAttributes();
        lp.width = (int) (0.9 * YUtils.getScreenWidth());
        lp.height = (int) (0.95 * YUtils.getScreenHeight());
        getDialog().getWindow().setAttributes(lp);
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        getDialog().getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
        return super.onCreateView(inflater, container, savedInstanceState);
    }

    @NonNull
    @Override
    public Dialog onCreateDialog(@Nullable Bundle savedInstanceState) {
        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
        LayoutInflater inflater = getActivity().getLayoutInflater();
        View view = inflater.inflate(R.layout.dialog_prescription_list, null);
        ButterKnife.bind(this, view);
        builder.setView(view);

        addPrescription = (AddPrescription) getArguments().getSerializable("prescription");
        patient = (Patient) getArguments().getSerializable("patient");
        prescriptionId = getArguments().getString("prescriptionId");

        tvPatientName.setText(patient.name);
        tvPatientSex.setText(patient.getGender());
        if(!TextUtils.isEmpty(patient.birthday)){
            tvPatientAge.setText(TimeUtils.getAgeDetail(patient.birthday));
        }
        tvAction.setText(addPrescription.action);
        tvHistory.setText(addPrescription.previous_history);
        tvAllergy.setText(addPrescription.allergic_history);
        tvDosage.setText(addPrescription.diagnose);

        initBillRecyclerView();

        return builder.create();
    }

    private void initBillRecyclerView(){
        mDrugSelectList = new ArrayList<>();
        List<CustomGroupDrug> drugList = addPrescription.drugs;
        for(int i=0;i<drugList.size();i++){
            List<Drug> childDrugs = drugList.get(i).list;
            if(childDrugs!=null&&childDrugs.size()>0){
                childDrugs.get(0).headTitle = i+1;
                mDrugSelectList.addAll(childDrugs);
            }
        }
        billAdapter = new BillAdapter(mDrugSelectList);
        rvPrescription.setLayoutManager(new LinearLayoutManager(getContext()));
        rvPrescription.setAdapter(billAdapter);

    }

    @OnClick(R.id.iv_prescription_list_close)
    public void closeDialog(View view){
        dismiss();
    }

    @OnClick(R.id.btn_prescription_toll)
    public void tollClick(View view){
        BillTollDialog billTollDialog = new BillTollDialog(addPrescription,prescriptionId);
        billTollDialog.show(getChildFragmentManager(),"bill");
    }
}
